
from game.entities.food_entity import FoodEntity
from game.entities.snake_entity import SnakeEntity
from game.config import *
import random

class GroundEntity:

    def __init__(self):
        self.snake=SnakeEntity()
        self.generate_food()
        pass

    def go(self):
        new_position=self.snake.next_position()
        with_eat=new_position==(self.food.r,self.food.c)
        print(f'with_eat:{with_eat}')
        self.snake.move(with_eat)
        if with_eat:
            self.generate_food()

    def generate_food(self):
        # 产生食物(随机判断一个位置，如果该位置不在蛇上，则产生食物，否则重新产生)
        if len(self.snake.body)==ROW_COUNT*COL_COUNT:
            raise Exception("游戏结束")
        
        while True:
            r = random.randint(0, ROW_COUNT)
            c = random.randint(0, COL_COUNT)
            if (r, c) not in self.snake.body:
                self.food=FoodEntity(r, c)
                print(f'generate food:{r},{c}')
                return
            
    def draw(self):
        self.snake.draw()
        if self.food:
            self.food.draw()
